home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997…eptember: Technology Seed / ATS Aug-Sept '97.toast / Navigation Services SDK / Examples / Sampler / Sampler ƒ / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  12.6 KB  |  521 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        menus.c
  3.  
  4.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  5.  
  6. */
  7.  
  8. #pragma segment AppSeg
  9.  
  10. #ifndef __SCRAP__
  11. #include <Scrap.h>
  12. #endif
  13.  
  14. #ifndef __DEVICES__
  15. #include <Devices.h>
  16. #endif
  17.  
  18. #ifndef __TOOLUTILS__
  19. #include <ToolUtils.h>
  20. #endif
  21.  
  22.  
  23. #ifndef Common_Defs
  24. #include "Common.h"
  25. #endif
  26.  
  27. extern Document* gDocumentList[kMaxDocumentCount];
  28. extern short    gDocumentCount;
  29. extern short    gCanUndoDrag;
  30. extern short    gQuit, gQuitting;
  31. extern Boolean    gNavServicesExists;
  32.  
  33. // prototypes:
  34. void SetItemEnable(MenuHandle theMenu, short theItem, short enable);
  35. short DoPaste(void);
  36. void DoAbout(void);
  37. void DoSelectDictionary(void);
  38. void DoSelectDirectory(void);
  39. void DoSelectVolume(void);
  40. void DoCreateFolder(void);
  41.  
  42.  
  43. // *****************************************************************************
  44. // *
  45. // *    SetItemEnable()
  46. // *
  47. // *****************************************************************************
  48. void SetItemEnable(MenuHandle theMenu, short theItem, short enable)
  49. {
  50.     if (enable)
  51.         EnableItem(theMenu,theItem);
  52.     else
  53.         DisableItem(theMenu,theItem);
  54. }
  55.  
  56.  
  57. // *****************************************************************************
  58. // *
  59. // *    AdjustMenus()
  60. // *
  61. // *****************************************************************************
  62. void AdjustMenus()
  63. {    
  64.     MenuHandle    theMenu;
  65.     Document*    theDocument;
  66.     short        teSelection = 0;
  67.     Str255        theStr;
  68.  
  69.     theDocument = IsDocumentWindow((WindowPtr)FrontWindow());
  70.     
  71.     if (theDocument->theTE != NULL)
  72.         teSelection = (theDocument) && ((**(theDocument->theTE)).selStart != (**(theDocument->theTE)).selEnd);
  73.  
  74.     theMenu = GetMenuHandle(idFileMenu);
  75.  
  76.     SetItemEnable(theMenu,NewItem,gDocumentCount < kMaxDocumentCount);
  77.     SetItemEnable(theMenu,OpenItem,gDocumentCount < kMaxDocumentCount);
  78.  
  79.     SetItemEnable(theMenu,CloseItem,theDocument != 0L);
  80.     SetItemEnable(theMenu,SaveItem,(theDocument) && (theDocument->dirty));
  81.     SetItemEnable(theMenu,RevertItem,(theDocument) && (theDocument->dirty) && (theDocument->fRefNum));
  82.     SetItemEnable(theMenu,SaveACopyItem,theDocument != 0L);
  83.     SetItemEnable(theMenu,DictionaryItem,gNavServicesExists);
  84.  
  85.     theMenu = GetMenuHandle(idEditMenu);
  86.  
  87.     GetIndString(theStr,MenuStringsID,gCanUndoDrag);
  88.     SetMenuItemText(theMenu,iUndo,theStr);
  89.     SetItemEnable(theMenu,iUndo,gCanUndoDrag != slCantUndo);
  90.  
  91.     SetItemEnable(theMenu,iCut,teSelection);
  92.     SetItemEnable(theMenu,iCopy,teSelection);
  93.     SetItemEnable(theMenu,iPaste,theDocument != 0L);
  94.     SetItemEnable(theMenu,iClear,teSelection);
  95.     SetItemEnable(theMenu,iSelectAll,theDocument != 0L);
  96.  
  97.     theMenu = GetMenuHandle(idUtilsMenu);
  98.     SetItemEnable(theMenu,iSelectDir,gNavServicesExists);
  99.     SetItemEnable(theMenu,iSelectVol,gNavServicesExists);
  100.     SetItemEnable(theMenu,iCreateFolder,gNavServicesExists);
  101. }
  102.  
  103.  
  104. // *****************************************************************************
  105. // *
  106. // *    DoPaste()
  107. // *
  108. // *****************************************************************************
  109. short DoPaste()
  110. {    
  111.     long    size, offset;
  112.     Handle    theData;
  113.  
  114.     size = GetScrap(0L,'UPRC',&offset);
  115.  
  116.     if (size <= 0)
  117.         return(0);
  118.  
  119.     theData = NewHandle(size);
  120.     GetScrap(theData,'UPRC',&offset);
  121.  
  122.     HLock(theData);
  123.     PutScrap(size,'test',*theData);
  124.  
  125.     HUnlock(theData);
  126.     DisposeHandle(theData);
  127.  
  128.     return noErr;
  129. }
  130.  
  131.  
  132. // *****************************************************************************
  133. // *
  134. // *    DoSelectDictionary()
  135. // *    
  136. // *****************************************************************************
  137. void DoSelectDictionary()
  138. {    
  139.     const OSType fileType = 'DICT';        // the file type to be chosen
  140.  
  141.     NavReplyRecord        theReply;
  142.     NavDialogOptions    dialogOptions;
  143.     OSErr                theErr = noErr;
  144.     NavEventProcUPP        eventProcUPP = NewNavEventProc(myEventProc);
  145.     
  146.     // use the default location for the dialog:
  147.     theErr = NavGetDefaultDialogOptions(&dialogOptions);
  148.  
  149.     //•• set the message field to 'message', to be define in .h file someday, put in STR#
  150.     Str255 message = "\pPlease find the dictionary file:";
  151.     BlockMoveData(message+1,dialogOptions.message+1,message[0]);
  152.     dialogOptions.message[0] = message[0];
  153.  
  154.     dialogOptions.allowPreviews = false;
  155.     dialogOptions.preferenceKey = kSelectFilePrefKey;
  156.     
  157.     theErr = NavChooseFile(    NULL,
  158.                             &theReply,
  159.                             &dialogOptions,
  160.                             eventProcUPP,
  161.                             NULL, 
  162.                             (NavCallBackUserData)&gDocumentList,                                    
  163.                             fileType);
  164.     
  165.     DisposeRoutineDescriptor(eventProcUPP);
  166.     
  167.     if ((theReply.validRecord)&&(theErr == noErr))
  168.         {
  169.         // grab the target FSSpec from the AEDesc:    
  170.         FSSpec    finalFSSpec;    
  171.         AEDesc     resultDesc;
  172.  
  173.         theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
  174.         if (theErr == noErr)
  175.             {
  176.             BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));    
  177.             // 'finalFSSpec' is the chosen file…
  178.             }
  179.             
  180.         theErr = NavDisposeReply(&theReply);
  181.         }
  182. }
  183.  
  184.  
  185. // *****************************************************************************
  186. // *
  187. // *    DoSelectDirectory()
  188. // *    
  189. // *****************************************************************************
  190. void DoSelectDirectory()
  191. {    
  192.     NavReplyRecord        theReply;
  193.     NavDialogOptions    dialogOptions;
  194.     OSErr                theErr = noErr;
  195.     NavEventProcUPP        eventProcUPP = NewNavEventProc(myEventProc);
  196.     
  197.     theErr = NavGetDefaultDialogOptions(&dialogOptions);
  198.     
  199.     //•• set the message field to 'message', to be define in .h file someday, put in STR#
  200.     Str255 message = "\pPlease select a directory:";
  201.     BlockMoveData(message+1,dialogOptions.message+1,message[0]);
  202.     dialogOptions.message[0] = message[0];
  203.     
  204.     dialogOptions.preferenceKey = kSelectFolderPrefKey;
  205.     
  206.     theErr = NavChooseFolder(    NULL,
  207.                                 &theReply,
  208.                                 &dialogOptions,
  209.                                 eventProcUPP, 
  210.                                 (NavCallBackUserData)&gDocumentList);
  211.     
  212.     DisposeRoutineDescriptor(eventProcUPP);
  213.  
  214.     if ((theReply.validRecord)&&(theErr == noErr))
  215.         {
  216.         // grab the target FSSpec from the AEDesc:    
  217.         FSSpec    finalFSSpec;    
  218.         AEDesc     resultDesc;
  219.  
  220.         theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
  221.         if (theErr == noErr)
  222.             {
  223.             BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));    
  224.             // 'finalFSSpec' is the selected directory…
  225.             }
  226.         
  227.         theErr = NavDisposeReply(&theReply);
  228.         }
  229. }
  230.  
  231.  
  232. // *****************************************************************************
  233. // *
  234. // *    DoSelectVolume()
  235. // *    
  236. // *****************************************************************************
  237. void DoSelectVolume()
  238. {    
  239.     NavReplyRecord        theReply;
  240.     NavDialogOptions    dialogOptions;
  241.     OSErr                theErr = noErr;
  242.     NavEventProcUPP        eventProcUPP = NewNavEventProc(myEventProc);
  243.     
  244.     // use the default location for the dialog:
  245.     theErr = NavGetDefaultDialogOptions(&dialogOptions);
  246.     
  247.     //•• set the message field to 'message', to be define in .h file someday, put in STR#
  248.     Str255 message = "\pPlease select a volume:";
  249.     BlockMoveData(message+1,dialogOptions.message+1,message[0]);
  250.     dialogOptions.message[0] = message[0];
  251.  
  252.     dialogOptions.preferenceKey = kSelectVolumePrefKey;
  253.     
  254.     theErr = NavChooseVolume(&theReply,
  255.                             &dialogOptions,
  256.                             eventProcUPP, 
  257.                             (NavCallBackUserData)&gDocumentList);
  258.     if ((theReply.validRecord)&&(theErr == noErr))
  259.         {
  260.         // grab the target FSSpec from the AEDesc:    
  261.         FSSpec    finalFSSpec;    
  262.         AEDesc     resultDesc;
  263.  
  264.         // there is only one selection here we get only the first AEDesc:
  265.         theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
  266.         if (theErr == noErr)
  267.             {
  268.             BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));
  269.             // 'finalFSSpec' is the chosen volume…
  270.             }
  271.         
  272.         theErr = NavDisposeReply(&theReply);
  273.         }
  274. }
  275.  
  276.  
  277. // *****************************************************************************
  278. // *
  279. // *    DoCreateFolder()
  280. // *    
  281. // *****************************************************************************
  282. void DoCreateFolder()
  283. {    
  284.     NavReplyRecord        theReply;
  285.     NavDialogOptions    dialogOptions;
  286.     OSErr                theErr = noErr;
  287.     NavEventProcUPP        eventProcUPP = NewNavEventProc(myEventProc);
  288.     
  289.     // use the default location for the dialog:
  290.     theErr = NavGetDefaultDialogOptions(&dialogOptions);
  291.     
  292.     //•• set the message field to 'message', to be define in .h file someday, put in STR#
  293.     Str255 message = "\pPlease create a folder:";
  294.     BlockMoveData(message+1,dialogOptions.message+1,message[0]);
  295.     dialogOptions.message[0] = message[0];
  296.  
  297.     dialogOptions.preferenceKey = kNewFolderPrefKey;
  298.     
  299.     theErr = NavNewFolder(    NULL,
  300.                             &theReply,
  301.                             &dialogOptions,
  302.                             eventProcUPP, 
  303.                             (NavCallBackUserData)&gDocumentList);
  304.     if (theReply.validRecord)
  305.         {
  306.         // grab the target FSSpec from the AEDesc:    
  307.         FSSpec    finalFSSpec;    
  308.         AEDesc     resultDesc;
  309.  
  310.         // there is only one selection here so we get the first AEDesc:
  311.         theErr = AECoerceDesc(&(theReply.selection),typeFSS,&resultDesc);
  312.         if (theErr == noErr)
  313.             {
  314.             BlockMove(*resultDesc.dataHandle,(void*)&finalFSSpec,sizeof(FSSpec));
  315.             // 'finalFSSpec' is the newly created folder…
  316.             }
  317.  
  318.         theErr = NavDisposeReply(&theReply);
  319.         }
  320. }
  321.  
  322.  
  323. // *****************************************************************************
  324. // *
  325. // *    DoAbout()
  326. // *    
  327. // *****************************************************************************
  328. void DoAbout()
  329. {
  330.     GrafPtr        savePort;
  331.     DialogPtr    aboutDialog;
  332.     short        itemHit = 0;
  333.     Handle        itemH;
  334.     short        itemType;
  335.     Rect        itemRect;
  336.     
  337.     SetCursor(&qd.arrow);
  338.     aboutDialog = GetNewDialog(rAboutID,nil,(WindowRef)-1);
  339.     
  340.     GetPort(&savePort);
  341.  
  342.     SetPort((GrafPtr)aboutDialog);
  343.  
  344.     AdornButton(aboutDialog,dOK);
  345.  
  346.     GetDialogItem(aboutDialog,iIconSuite,&itemType,&itemH,&itemRect);
  347.     DrawIconSuite(rIconSuite,itemRect);
  348.                     
  349.     do ModalDialog(nil,&itemHit);
  350.  
  351.     while(itemHit != dOK);
  352.     
  353.     SetPort(savePort);
  354.     DisposeDialog(aboutDialog);
  355. }
  356.  
  357.  
  358. // *****************************************************************************
  359. // *
  360. // *    DoMenuCommand()
  361. // *
  362. // *****************************************************************************
  363. void DoMenuCommand(long select)
  364. {    
  365.     short        theMenuID, theItem;
  366.     MenuHandle    theMenu;
  367.     Str255        theName;
  368.     WindowPtr    theWindow;
  369.     Document*    theDocument = nil;
  370.     OSStatus    theErr = noErr;
  371.  
  372.     theDocument = IsDocumentWindow(theWindow = (WindowPtr)FrontWindow());
  373.  
  374.     theItem   = LoWord(select);
  375.     theMenuID = HiWord(select);
  376.     theMenu   = GetMenuHandle(theMenuID);
  377.     switch(theMenuID)
  378.         {
  379.         case idAppleMenu:
  380.             switch(theItem)
  381.                 {
  382.                 case AboutItem:
  383.                     DoAbout();
  384.                     break;
  385.                 default:
  386.                     GetMenuItemText(GetMenuHandle(idAppleMenu),theItem,(unsigned char*)&theName);
  387.                     OpenDeskAcc(theName);
  388.                 }
  389.             break;
  390.             
  391.         case idFileMenu:
  392.             switch(theItem)
  393.                 {
  394.                 case NewItem:
  395.                     DoNewDocument(false);
  396.                     AdjustMenus();
  397.                     break;
  398.                     
  399.                 case OpenItem:
  400.                     if (gNavServicesExists)
  401.                         (void)DoOpenDocument();
  402.                     else    
  403.                         (void)DoOpenDocumentTheOldWay();
  404.                     AdjustMenus();        
  405.                     break;
  406.                     
  407.                 case CloseItem:
  408.                     if (theDocument)
  409.                         {
  410.                         CloseDocument(theDocument,false);
  411.                         AdjustMenus();
  412.                         DrawMenuBar();
  413.                         }
  414.                     break;
  415.                     
  416.                 case SaveItem:
  417.                     if (theDocument)
  418.                         DoSaveDocument(theDocument);
  419.                     break;
  420.                 
  421.                 case SaveACopyItem:
  422.                     if (theDocument)
  423.                         {
  424.                         if (gNavServicesExists)
  425.                             (void)SaveACopyDocument(theDocument);
  426.                         else
  427.                             (void)SaveACopyDocumentTheOldWay(theDocument);
  428.                         }
  429.                     break;
  430.     
  431.                 case RevertItem:
  432.                     DisableUndoDrag();
  433.                     if (theDocument)
  434.                         {
  435.                         if (gNavServicesExists)
  436.                             DoRevertDocument(theDocument);
  437.                         else
  438.                             DoRevertDocumentTheOldWay(theDocument);
  439.                         }
  440.                     break;
  441.  
  442.                 case DictionaryItem:
  443.                     DoSelectDictionary();
  444.                     break;
  445.  
  446.                 case QuitItem:
  447.                     gQuitting = true;
  448.                     while ((gQuitting) && (theDocument = IsDocumentWindow((WindowPtr)FrontWindow())))
  449.                         CloseDocument(theDocument,true);
  450.                     if (gQuitting)
  451.                         gQuit = true;
  452.                     break;
  453.                 }
  454.             break;
  455.             
  456.         case idEditMenu:
  457.             switch(theItem)
  458.                 {
  459.                 case iUndo:
  460.                     DoUndoDrag();
  461.                     break;
  462.                     
  463.                 case iCut:
  464.                     if ((theDocument)&&(theDocument->theTE != NULL))
  465.                         {
  466.                         DisableUndoDrag();
  467.                         myTECut(theDocument->theTE);
  468.                         }
  469.                     break;
  470.                     
  471.                 case iCopy:
  472.                     if (theDocument)
  473.                         TECopy(theDocument->theTE);
  474.                     break;
  475.                     
  476.                 case iPaste:
  477.                     if ((theDocument)&&(theDocument->theTE != NULL))
  478.                         if (!DoPaste())
  479.                             {
  480.                             DisableUndoDrag();
  481.                             myTEPaste(theDocument->theTE,0L,0L);
  482.                             }
  483.                     break;
  484.                     
  485.                 case iClear:
  486.                     if ((theDocument)&&(theDocument->theTE != NULL))
  487.                         {
  488.                         DisableUndoDrag();
  489.                         TEDelete(theDocument->theTE);
  490.                         }
  491.                     break;
  492.                     
  493.                 case iSelectAll:
  494.                     if ((theDocument)&&(theDocument->theTE != NULL))
  495.                         DoSelectAllDocument(theDocument);
  496.                     break;
  497.                 }
  498.  
  499.             case idUtilsMenu:
  500.                 switch (theItem)
  501.                     {
  502.                     case iSelectDir:
  503.                         DoSelectDirectory();
  504.                         break;
  505.                     case iSelectVol:
  506.                         DoSelectVolume();
  507.                         break;
  508.                     case iCreateFolder:
  509.                         DoCreateFolder();
  510.                         break;
  511.                     }
  512.         }        
  513.  
  514.     if (theDocument = IsDocumentWindow((WindowPtr)FrontWindow()))
  515.         if (theDocument->theTE != NULL)
  516.             TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
  517.  
  518.     HiliteMenu(0);
  519. }
  520.  
  521.